package com.huidz.calendar.activity;

import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.transition.Slide;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.huidz.calendar.R;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.DataCleanManager;
import com.huidz.calendar.util.Url;

public class SystemActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView comeback;

    private Switch jieshou, wifi;
    private TextView txtext, qtmrtext, mzkstext, qkhctext;
    private RelativeLayout user_out, wdfk, wdjnr;

    private int init = 0, inits = 0;
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.fragment_system);
        inti();
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        context = SystemActivity.this;
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        jieshou = findViewById(R.id.jieshou);
        wifi = findViewById(R.id.wifi);
        txtext = findViewById(R.id.txtext);
        qtmrtext = findViewById(R.id.qtmrtext);
        mzkstext = findViewById(R.id.mzkstext);
        qkhctext = findViewById(R.id.qkhctext);
        user_out = findViewById(R.id.user_out);
        if(Url.USER_TOKEN.equals(""))
            user_out.setVisibility(View.GONE);
        user_out.setOnClickListener(this);
        wdfk = findViewById(R.id.wdfk);
        wdfk.setOnClickListener(this);
        qkhctext.setText(getCacheSize(context));
        wdjnr = findViewById(R.id.wdjnr);
        wdjnr.setOnClickListener(this);
    }


    private String getCacheSize(Context context) {
        String str = "";
        try {
            str = DataCleanManager.getTotalCacheSize(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                onBackPressed();
                break;

            case R.id.user_out:
                Url.USER_HEAD="";
                Url.USER_ID="";
                Url.USER_NAME="";
                Url.USER_TOKEN="";
                Url.USER_MOBILE="";
                onBackPressed();
                mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);  //名称和类型
                mEditor = mSharedPreferences.edit();
                mEditor.putString("user_token", "");
                mEditor.putString("user_name","");
                mEditor.putString("user_mobile","");
                mEditor.putString("user_head","");
                mEditor.putString("user_id", "");
                mEditor.apply();
                mEditor.commit();
                break;
            case R.id.wdfk:
                getCacheSize(context);
                if (DataCleanManager.getsize > 0)
                    if (init == 0)
                        show(context);
                    else
                        Apiutils.SetToast(context, R.string.deling);
                else
                    Apiutils.SetToast(context, R.string.nodel);
                break;
            case R.id.wdjnr:
                Intent music = new Intent(SystemActivity.this, MusicActivity.class);
                startActivity(music, ActivityOptions.makeSceneTransitionAnimation(SystemActivity.this).toBundle());
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (MusicActivity.music_name != null) {
            txtext.setText("当前铃声:" + MusicActivity.music_name);
        }
    }

    private AlertDialog.Builder ibuilder;

    private void show(final Context context) {
        ibuilder = new AlertDialog.Builder(context);
        ibuilder.setTitle("清除缓存");
        ibuilder.setMessage("清除缓存会导致下载的内容删除，是否确定?");
        ibuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        ibuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                init++;
                DataCleanManager.clearAllCache(context);
                dialog.dismiss();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        String getnum = getCacheSize(context);
                        qkhctext.setText(getnum);
                        init = 0;
                    }
                }, 3000);
            }
        });

        Dialog dialog = ibuilder.create();
        dialog.show();
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        WindowManager.LayoutParams p =
                dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.9
        dialog.getWindow().setAttributes(p);    //设置生效
    }

}

